package com.suredy.security.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import com.suredy.core.service.BaseSrvWithEntity;
import com.suredy.security.entity.RoleEntity;
import com.suredy.security.entity.User2RoleEntity;
import com.suredy.security.entity.UserEntity;
import com.suredy.security.model.Role;

@Service("RoleSrv")
public class RoleSrv extends BaseSrvWithEntity<RoleEntity> {
	@Override
	public DetachedCriteria getDc(RoleEntity t) {
		DetachedCriteria dc = super.getDc(t);
		if (t == null) {
			return dc;
		}
		if (!StringUtils.isBlank(t.getId())) {
			dc.add(Restrictions.eq("id", t.getId()));
		}
		
		if (!StringUtils.isBlank(t.getName())) {
			dc.add(Restrictions.eq("name", t.getName()));
		}
		
		if (!StringUtils.isBlank(t.getUniqueCode())) {
			dc.add(Restrictions.eq("uniqueCode", t.getUniqueCode()));
		}

		return dc;
	}
	
	public Long Count() {
		String ql = "SELECT COUNT(*) FROM RoleEntity T WHERE T.buildIn=0";
		Object ret = this.readSingleByQL(ql);
		return (Long) ret;
	}
	
	public List<Role> getAll() {
		String ql = "SELECT T FROM RoleEntity T WHERE T.buildIn=0";
		@SuppressWarnings("unchecked")
		List<RoleEntity> pos = (List<RoleEntity>) this.readByQL(ql);
		List<Role> ret = new ArrayList<Role>();
		for (RoleEntity po : pos) {
			ret.add(po.toVO());
		}
		return ret;
	}
	
	public List<Role> getAll(Integer page, Integer size) {
		String ql = "SELECT T FROM RoleEntity T WHERE T.buildIn=0";
		Object[] paras = null;
		@SuppressWarnings("unchecked")
		List<RoleEntity> pos = (List<RoleEntity>) this.readByQL(ql, page, size, paras);
		List<Role> ret = new ArrayList<Role>();
		for (RoleEntity po : pos) {
			ret.add(po.toVO());
		}
		return ret;
	}

	public Role getById(String id) {
		if (StringUtils.isBlank(id)) {
			return null;
		}

		RoleEntity r = this.get(id);

		if (r == null)
			return null;

		return r.toVO();
	}

	public Role getByName(String name) {
		if (StringUtils.isBlank(name)) {
			return null;
		}
		RoleEntity search = new RoleEntity();
		search.setName(name);
		RoleEntity ret = this.readSingleByEntity(search);
		if (ret == null) {
			return null;
		}
		return ret.toVO();
	}
	
	public Role getByCode(String uniqueCode) {
		if (StringUtils.isBlank(uniqueCode)) {
			return null;
		}
		RoleEntity search = new RoleEntity();
		search.setUniqueCode(uniqueCode);
		RoleEntity ret = this.readSingleByEntity(search);
		if (ret == null) {
			return null;
		}
		return ret.toVO();
	}
	
	public List<Role> getByUserId(String userId) {
		if (StringUtils.isBlank(userId))
			return null;

		UserEntity search = new UserEntity();
		search.setId(userId);

		DetachedCriteria dc = DetachedCriteria.forClass(User2RoleEntity.class, "T");
		dc.add(Restrictions.eq("user", search));

		List<?> lst = this.readByCriteria(dc);

		if (lst == null)
			return null;

		List<Role> ret = new ArrayList<Role>();

		for (Object obj : lst) {
			User2RoleEntity tmp = (User2RoleEntity) obj;

			if (tmp.getRole() != null)
				ret.add(tmp.getRole().toVO());
		}

		return ret.isEmpty() ? null : ret;
	}
}
